package com.lifeverse.repository;

import com.lifeverse.entity.LifeEntity;
import com.lifeverse.entity.LifeEntityRelationship;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * 生命体关系数据访问层
 */
@Repository
public interface LifeEntityRelationshipRepository extends JpaRepository<LifeEntityRelationship, Long> {
    
    /**
     * 根据源生命体查找所有关系
     */
    List<LifeEntityRelationship> findBySourceEntity(LifeEntity sourceEntity);
    
    /**
     * 根据目标生命体查找所有关系
     */
    List<LifeEntityRelationship> findByTargetEntity(LifeEntity targetEntity);
    
    /**
     * 查找两个生命体之间的关系
     */
    Optional<LifeEntityRelationship> findBySourceEntityAndTargetEntity(
            LifeEntity sourceEntity, LifeEntity targetEntity);
    
    /**
     * 根据关系类型查找关系
     */
    List<LifeEntityRelationship> findByRelationshipType(
            LifeEntityRelationship.RelationshipType relationshipType);
    
    /**
     * 根据关系状态查找关系
     */
    List<LifeEntityRelationship> findByRelationshipStatus(
            LifeEntityRelationship.RelationshipStatus relationshipStatus);
    
    /**
     * 查找活跃的关系
     */
    @Query("SELECT r FROM LifeEntityRelationship r WHERE r.relationshipStatus = 'ACTIVE'")
    List<LifeEntityRelationship> findActiveRelationships();
    
    /**
     * 根据关系强度范围查找关系
     */
    List<LifeEntityRelationship> findByRelationshipStrengthBetween(
            BigDecimal minStrength, BigDecimal maxStrength);
    
    /**
     * 查找强关系（强度大于70）
     */
    @Query("SELECT r FROM LifeEntityRelationship r WHERE r.relationshipStrength >= 70.0")
    List<LifeEntityRelationship> findStrongRelationships();
    
    /**
     * 查找某个生命体的所有关系（包括作为源和目标）
     */
    @Query("SELECT r FROM LifeEntityRelationship r WHERE r.sourceEntity = :entity OR r.targetEntity = :entity")
    List<LifeEntityRelationship> findAllRelationshipsByEntity(@Param("entity") LifeEntity entity);
    
    /**
     * 查找某个生命体的活跃关系
     */
    @Query("SELECT r FROM LifeEntityRelationship r WHERE (r.sourceEntity = :entity OR r.targetEntity = :entity) AND r.relationshipStatus = 'ACTIVE'")
    List<LifeEntityRelationship> findActiveRelationshipsByEntity(@Param("entity") LifeEntity entity);
    
    /**
     * 根据生命体和关系类型查找关系
     */
    @Query("SELECT r FROM LifeEntityRelationship r WHERE (r.sourceEntity = :entity OR r.targetEntity = :entity) AND r.relationshipType = :type")
    List<LifeEntityRelationship> findRelationshipsByEntityAndType(
            @Param("entity") LifeEntity entity, 
            @Param("type") LifeEntityRelationship.RelationshipType type);
    
    /**
     * 统计某个生命体的关系数量
     */
    @Query("SELECT COUNT(r) FROM LifeEntityRelationship r WHERE r.sourceEntity = :entity OR r.targetEntity = :entity")
    long countRelationshipsByEntity(@Param("entity") LifeEntity entity);
    
    /**
     * 统计某种类型的关系数量
     */
    long countByRelationshipType(LifeEntityRelationship.RelationshipType relationshipType);
    
    /**
     * 查找最近建立的关系
     */
    List<LifeEntityRelationship> findTop10ByOrderByEstablishedAtDesc();
    
    /**
     * 查找最近互动的关系
     */
    List<LifeEntityRelationship> findTop10ByOrderByLastInteractionAtDesc();
    
    /**
     * 查找长时间未互动的关系
     */
    @Query("SELECT r FROM LifeEntityRelationship r WHERE r.lastInteractionAt < :threshold AND r.relationshipStatus = 'ACTIVE'")
    List<LifeEntityRelationship> findInactiveRelationships(@Param("threshold") LocalDateTime threshold);
    
    /**
     * 根据信任度范围查找关系
     */
    List<LifeEntityRelationship> findByTrustLevelBetween(BigDecimal minTrust, BigDecimal maxTrust);
    
    /**
     * 查找高信任度关系
     */
    @Query("SELECT r FROM LifeEntityRelationship r WHERE r.trustLevel >= 80.0")
    List<LifeEntityRelationship> findHighTrustRelationships();
    
    /**
     * 根据互动频率排序查找关系
     */
    List<LifeEntityRelationship> findTop20ByOrderByInteractionFrequencyDesc();
    
    /**
     * 查找网络权重最高的关系
     */
    List<LifeEntityRelationship> findTop50ByOrderByNetworkWeightDesc();
    
    /**
     * 分页查询关系
     */
    Page<LifeEntityRelationship> findByRelationshipStatus(
            LifeEntityRelationship.RelationshipStatus status, Pageable pageable);
    
    /**
     * 查找双向关系
     */
    List<LifeEntityRelationship> findByIsBidirectionalTrue();
    
    /**
     * 查找单向关系
     */
    List<LifeEntityRelationship> findByIsBidirectionalFalse();
    
    /**
     * 根据影响评分范围查找关系
     */
    List<LifeEntityRelationship> findByImpactScoreBetween(BigDecimal minScore, BigDecimal maxScore);
    
    /**
     * 查找正面影响的关系
     */
    @Query("SELECT r FROM LifeEntityRelationship r WHERE r.impactScore > 0")
    List<LifeEntityRelationship> findPositiveImpactRelationships();
    
    /**
     * 查找负面影响的关系
     */
    @Query("SELECT r FROM LifeEntityRelationship r WHERE r.impactScore < 0")
    List<LifeEntityRelationship> findNegativeImpactRelationships();
    
    /**
     * 根据标签查找关系
     */
    @Query("SELECT r FROM LifeEntityRelationship r WHERE r.tags LIKE %:tag%")
    List<LifeEntityRelationship> findByTagsContaining(@Param("tag") String tag);
    
    /**
     * 查找需要维护的关系（质量低或信任度低）
     */
    @Query("SELECT r FROM LifeEntityRelationship r WHERE (r.relationshipQuality < 40.0 OR r.trustLevel < 40.0) AND r.relationshipStatus = 'ACTIVE'")
    List<LifeEntityRelationship> findRelationshipsNeedingMaintenance();
    
    /**
     * 根据建立时间范围查找关系
     */
    List<LifeEntityRelationship> findByEstablishedAtBetween(LocalDateTime startTime, LocalDateTime endTime);
    
    /**
     * 查找某个时间段内最后互动的关系
     */
    List<LifeEntityRelationship> findByLastInteractionAtBetween(LocalDateTime startTime, LocalDateTime endTime);
}